package com.pubinfo.passbook.common.service.master.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pubinfo.passbook.common.entity.TRabbitmqOrg;
import com.pubinfo.passbook.common.entity.TRabbitmqOrgConvert;
import com.pubinfo.passbook.common.mapper.TRabbitmqOrgConvertMapper;
import com.pubinfo.passbook.common.mapper.TRabbitmqOrgMapper;
import com.pubinfo.passbook.common.model.bo.rabbitmq.RabbitMqRoot;
import com.pubinfo.passbook.common.service.master.TRabbitmqOrgConvertService;
import com.pubinfo.passbook.common.service.master.TRabbitmqOrgService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xuhong
 * @since 2021-10-22
 */
@Service
public class TRabbitmqOrgServiceImpl extends ServiceImpl<TRabbitmqOrgMapper, TRabbitmqOrg> implements TRabbitmqOrgService {
  
  @Resource
  TRabbitmqOrgConvertService orgConvertService;
  
  @Override
  @Transactional
  public Boolean insertOrg(JSONObject json) {
    if (json != null) {
      RabbitMqRoot root = JSONObject.toJavaObject(json, RabbitMqRoot.class);
      if ("delete".equals(json.getString("handle"))) {
        Object orgId = JSONPath.eval(json, "$.context.orgId");
        if (orgId != null) {
          return removeById(orgId.toString());
        }
      } else {
        TRabbitmqOrg org = new TRabbitmqOrg(root);
        return saveOrUpdate(org) && orgConvertService.processOrgMsg(root);
      }
    }
    
    return false;
  }
  
}
